package at.fhj.antesk.steamvaluecalc.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.Map;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableModel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import at.fhj.antesk.steamvaluecalc.Account;
import at.fhj.antesk.steamvaluecalc.Game;
import at.fhj.antesk.steamvaluecalc.GameDataRetriever;
import at.fhj.antesk.steamvaluecalc.interfaces.Retriever;
import at.fhj.antesk.steamvaluecalc.utils.HelperUtil;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JTextField;

public class Overview extends JFrame {

	private static final long serialVersionUID = -2117485508359785811L;
	private JPanel contentPane;
	private JPanel mainPanel;
	private JPanel accountImagePanel;
	private JPanel accountStatisticsPanel_1;
	private JPanel accountSelectedGamePanel_1;
	private JPanel gameImagePanel;
	private JPanel selectedGameDetailsPanel;
	private JLabel lblAccountName;
	private JLabel lblTotalPlaytime;
	private JLabel lblHours;
	private JLabel lblTotalMoneySpent;
	private JLabel lblMoney;
	private JLabel lblEntertainmentCostPer;
	private JLabel lblCost;
	private JLabel lblHoursPlayed;
	private JLabel lblPricePayed;
	private JLabel lblEntertainmentCostPer_1;
	private JLabel lblEntertainmentCostPerHourGame;
	private JLabel lblNumberOfGames;
	private JLabel lblGamenumber;
	private JMenu mnAccount;
	private JMenu mnPriceList;
	private JMenu mnHelp;
	private JMenuItem mntmChangeAccount;
	private JMenuItem mntmRefreshPriceList;
	private JMenuItem mntmUpdatePrices;
	private JMenuItem mntmExit;
	private JMenuItem mntmHowToUse;
	private JMenuItem mntmAboutSteamProfile;
	private JSeparator separator;
	private JSeparator separator_1;
	private JList list;

	private Retriever retriever = new GameDataRetriever();
	private boolean hasNoAlias;
	double totalPriceOfAllGames;
	double totalHoursOfAllGames;
	double totalHourValue;

	private String accountIdentification;
	private Map<String, String> configsMap = retriever.getConfigsMap();
	private Desktop desktop = Desktop.getDesktop();
	private LoadingWindow loadingWindow = new LoadingWindow();
	private JProgressBar progressBar = loadingWindow.getProgressBar();
	private Thread priceRetrieving;
	private Game selectedGame = new Game();

	private static Account account;
	private static Logger logger = LoggerFactory.getLogger(Overview.class);
	private JTextField txtHours;
	private JTextField txtPrice;
	private JMenuItem mntmVisitStorePage;

	public static Account getAccount() {
		return account;
	}

	private void fillListAndLabelsForAccount() {
		lblAccountName.setText(account.getName());
		lblHours.setText(HelperUtil.round(totalHoursOfAllGames, 2) + " hours");
		lblMoney.setText(HelperUtil.round(totalPriceOfAllGames, 2) + " " + account.getGames().get(0).getCurrency());
		lblGamenumber.setText(Integer.toString(account.getGames().size()));
		lblCost.setText(HelperUtil.round(totalHourValue, 2) + " " + account.getGames().get(0).getCurrency());
		Vector<String> gameListForJList = retriever.createGameListForJList(account.getGames());
		list.setListData(gameListForJList);
	}

	public Overview() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Overview.class.getResource("/resources/spicon.gif")));
		setBackground(Color.DARK_GRAY);
		setupJFrameProperties();

		retriever.setMainFrame(this);
		accountIdentification = configsMap.get("account");
		account = retriever.createAccount(accountIdentification);

		calculateHoursAndPricesTotals();

		setupMenu();
		setupContentPane();

		JPanel accountDataPanel = createAccountDataPanel();
		JPanel accountStatisticsPanel = createAccountStatisticsPanel();
		JPanel accountSelectedGamePanel = createAccountSelectedGamePanel();

		setupLabelsAndButtonsWithAccountPanels(accountDataPanel, accountStatisticsPanel, accountSelectedGamePanel);

		setupGameListPanel();

		fillListAndLabelsForAccount();

		validate();
		repaint();

		list.revalidate();
	}

	private void calculateHoursAndPricesTotals() {
		totalPriceOfAllGames = HelperUtil.sumUpPricesFromGameList(account.getGames());
		totalHoursOfAllGames = HelperUtil.sumUpHoursFromGameList(account.getGames());
		totalHourValue = HelperUtil.calculateHourValueForGame(totalPriceOfAllGames, totalHoursOfAllGames);
	}

	private void setupLabelsAndButtonsWithAccountPanels(JPanel accountDataPanel, JPanel accountStatisticsPanel,
			JPanel accountSelectedGamePanel) {
		mainPanel.setLayout(null);
		mainPanel.add(accountDataPanel);
		accountDataPanel.setLayout(null);

		// Account Image and Name

		accountImagePanel = new JPanel() {
			private static final long serialVersionUID = 1179701111419210772L;

			@Override
			public void paint(Graphics g) {
				try {
					String iconUrl = "";
					if (null == account.getAvatarIconUrl()) {
						iconUrl = configsMap.get("overview.default.avatar.icon.url");
					} else {
						iconUrl = account.getAvatarIconUrl();
					}
					BufferedImage imageFromURL = ImageIO.read(new URL(iconUrl));
					g.drawImage(imageFromURL, 0, 0, null);
				} catch (MalformedURLException e) {
					logger.error("Url invalid for account image", e);
				} catch (IOException e) {
					logger.error("File can't be read from this URL", e);
				}
			}

		};
		accountImagePanel.setBounds(12, 22, 184, 184);
		accountDataPanel.add(accountImagePanel);
		accountImagePanel.setLayout(new BorderLayout(0, 0));

		lblAccountName = new JLabel(account.getName());
		lblAccountName.setToolTipText("Open account page");
		lblAccountName.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		lblAccountName.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				try {
					URI uri = URI.create("http://steamcommunity.com/id/" + accountIdentification);
					desktop.browse(uri);
				} catch (Exception e) {
					logger.error("Error occured while opening browser", e);
				}
			}
		});
		lblAccountName.setVerticalAlignment(SwingConstants.BOTTOM);
		lblAccountName.setForeground(Color.LIGHT_GRAY);
		lblAccountName.setFont(new Font("Dialog", Font.PLAIN, 22));
		lblAccountName.setBounds(214, 156, 224, 50);
		accountDataPanel.add(lblAccountName);
		mainPanel.add(accountStatisticsPanel);
		accountStatisticsPanel.setLayout(new GridLayout(8, 1, 0, 0));

		// Account Statistics

		lblNumberOfGames = new JLabel(configsMap.get("overview.label.number.of.games"));
		lblNumberOfGames.setVerticalAlignment(SwingConstants.BOTTOM);
		lblNumberOfGames.setForeground(Color.WHITE);
		accountStatisticsPanel_1.add(lblNumberOfGames);

		lblGamenumber = new JLabel(Integer.toString(account.getGames().size()));
		lblGamenumber.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblGamenumber.setForeground(Color.WHITE);
		lblGamenumber.setVerticalAlignment(SwingConstants.TOP);
		accountStatisticsPanel_1.add(lblGamenumber);

		lblTotalPlaytime = new JLabel(configsMap.get("overview.label.total.playtime"));
		lblTotalPlaytime.setFont(new Font("Dialog", Font.BOLD, 12));
		lblTotalPlaytime.setVerticalAlignment(SwingConstants.BOTTOM);
		lblTotalPlaytime.setForeground(Color.WHITE);
		accountStatisticsPanel.add(lblTotalPlaytime);

		lblHours = new JLabel(HelperUtil.round(totalHoursOfAllGames, 2) + " hours");
		lblHours.setForeground(Color.WHITE);
		lblHours.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblHours.setVerticalAlignment(SwingConstants.TOP);
		accountStatisticsPanel.add(lblHours);

		lblTotalMoneySpent = new JLabel(configsMap.get("overview.label.total.cost"));
		lblTotalMoneySpent.setFont(new Font("Dialog", Font.BOLD, 12));
		lblTotalMoneySpent.setForeground(Color.WHITE);
		lblTotalMoneySpent.setVerticalAlignment(SwingConstants.BOTTOM);
		accountStatisticsPanel.add(lblTotalMoneySpent);

		lblMoney = new JLabel(HelperUtil.round(totalPriceOfAllGames, 2) + " " + selectedGame.getCurrency());
		lblMoney.setForeground(Color.WHITE);
		lblMoney.setVerticalAlignment(SwingConstants.TOP);
		lblMoney.setFont(new Font("Dialog", Font.PLAIN, 12));
		accountStatisticsPanel.add(lblMoney);

		lblEntertainmentCostPer = new JLabel(configsMap.get("overview.label.total.entertainment.cost.per.hour"));
		lblEntertainmentCostPer.setFont(new Font("Dialog", Font.BOLD, 12));
		lblEntertainmentCostPer.setForeground(Color.WHITE);
		lblEntertainmentCostPer.setVerticalAlignment(SwingConstants.BOTTOM);
		accountStatisticsPanel.add(lblEntertainmentCostPer);

		lblCost = new JLabel(HelperUtil.round(totalHourValue, 2) + " EUR");
		lblCost.setForeground(Color.WHITE);
		lblCost.setVerticalAlignment(SwingConstants.TOP);
		lblCost.setFont(new Font("Dialog", Font.PLAIN, 12));
		accountStatisticsPanel.add(lblCost);
		mainPanel.add(accountSelectedGamePanel);
		accountSelectedGamePanel_1.setLayout(null);

		gameImagePanel = new JPanel() {
			private static final long serialVersionUID = -3349220337324076170L;

			@Override
			public void paint(Graphics g) {
				try {
					String iconUrl = "";
					if (null == selectedGame.getLogo()) {
						URL resource = this.getClass().getResource("/resources/game.jpg");
						iconUrl = resource.toExternalForm();
					} else {
						iconUrl = selectedGame.getLogo();
					}
					BufferedImage imageFromURL = ImageIO.read(new URL(iconUrl));
					g.drawImage(imageFromURL, 0, 0, null);
				} catch (MalformedURLException e) {
					logger.error("URL invalid", e);
				} catch (IOException e) {
					logger.debug("No picture found under the given URL: {} - Using default image.", selectedGame.getLogo());
					URL resource = this.getClass().getResource("/resources/game.jpg");
					String iconUrl = resource.toExternalForm();
					BufferedImage imageFromURL;
					try {
						imageFromURL = ImageIO.read(new URL(iconUrl));
						g.drawImage(imageFromURL, 0, 0, null);
					} catch (Exception e1) {
						logger.error("An error occured while the default image was drawn.", e1);
					}
				}
			}

		};
		gameImagePanel.setToolTipText("Open the community page for this game");
		gameImagePanel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		gameImagePanel.setBounds(18, 19, 184, 69);
		gameImagePanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				try {
					if (selectedGame.getId().intValue() != 0) {
						URI uri = URI.create("http://steamcommunity.com/app/" + selectedGame.getId());
						desktop.browse(uri);
					}
				} catch (IOException e) {
					logger.error("Error while opening browser for game", e);
				}
			}
		});
		accountSelectedGamePanel_1.add(gameImagePanel);
	}

	private JPanel createAccountSelectedGamePanel() {
		accountSelectedGamePanel_1 = new JPanel();
		accountSelectedGamePanel_1.setBounds(242, 243, 220, 244);
		accountSelectedGamePanel_1.setBorder(new TitledBorder(new LineBorder(new Color(192, 192, 192)), configsMap
				.get("overview.panel.selected.game.title"), TitledBorder.LEADING, TitledBorder.TOP, null,
				new Color(255, 255, 255)));
		accountSelectedGamePanel_1.setBackground(Color.DARK_GRAY);
		return accountSelectedGamePanel_1;
	}

	private JPanel createAccountStatisticsPanel() {
		accountStatisticsPanel_1 = new JPanel();
		accountStatisticsPanel_1.setBounds(12, 243, 220, 244);
		accountStatisticsPanel_1
				.setBorder(new TitledBorder(new LineBorder(new Color(192, 192, 192)), configsMap
						.get("overview.panel.account.statistics"), TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255,
						255, 255)));
		accountStatisticsPanel_1.setBackground(Color.DARK_GRAY);
		return accountStatisticsPanel_1;
	}

	private JPanel createAccountDataPanel() {
		JPanel accountDataPanel = new JPanel();
		accountDataPanel.setFont(new Font("Dialog", Font.PLAIN, 12));
		accountDataPanel.setBounds(12, 12, 450, 219);
		accountDataPanel.setBorder(new TitledBorder(new LineBorder(new Color(192, 192, 192)), configsMap
				.get("overview.panel.account.data"), TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 255, 255)));
		accountDataPanel.setBackground(Color.DARK_GRAY);
		return accountDataPanel;
	}

	private void setupJFrameProperties() {
		setResizable(false);
		setTitle(configsMap.get("overview.frame.title"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 875, 550);
		setLocationRelativeTo(null);
		setVisible(true);
		validate();
	}

	private void setupContentPane() {
		contentPane = new JPanel();
		contentPane.setBackground(Color.DARK_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		mainPanel = new JPanel();
		mainPanel.setBounds(0, 0, 869, 499);
		mainPanel.setBackground(Color.DARK_GRAY);
		contentPane.add(mainPanel);
	}

	private void setupMenu() {
		JMenuBar menuBar = new JMenuBar();
		menuBar.setFont(new Font("Segoe UI", Font.PLAIN, 12));
		setJMenuBar(menuBar);

		mnAccount = new JMenu(configsMap.get("overview.menu.account"));
		mnAccount.setFont(new Font("Dialog", Font.PLAIN, 12));
		menuBar.add(mnAccount);

		mntmChangeAccount = new JMenuItem(configsMap.get("overview.menu.item.changeaccount"));
		mntmChangeAccount.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				try {
					String changedAccount = JOptionPane.showInputDialog(null,
							configsMap.get("overview.menu.changeaccount.inputdialog.text"),
							configsMap.get("overview.menu.changeaccount.inputdialog.title"), JOptionPane.QUESTION_MESSAGE, null,
							null, accountIdentification).toString();

					String prefix = configsMap.get("overview.account.prefix");
					if (changedAccount.startsWith(prefix)) {
						logger.debug("ID: " + changedAccount + " entered instead of alias");
						hasNoAlias = true;
					} else {
						hasNoAlias = false;
					}

					if (!changedAccount.equals(accountIdentification)) {
						accountIdentification = changedAccount;
						logger.debug("Loading game list for account: " + accountIdentification);

						account = retriever.createAccount(accountIdentification);

						calculateHoursAndPricesTotals();
						fillListAndLabelsForAccount();

						accountImagePanel.repaint();
					}
				} catch (NullPointerException e) {
					logger.trace("Silent catch for NullPointerException in {}. Name was not changed.", arg0.getActionCommand(), e);
				}

			}
		});
		mntmChangeAccount.setFont(new Font("Dialog", Font.PLAIN, 12));
		mnAccount.add(mntmChangeAccount);

		separator = new JSeparator();
		mnAccount.add(separator);

		mntmExit = new JMenuItem(configsMap.get("overview.menu.item.exit"));
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				logger.debug("Application ended with Exit button");
				System.exit(0);
			}
		});
		mntmExit.setFont(new Font("Dialog", Font.PLAIN, 12));
		mnAccount.add(mntmExit);

		mnPriceList = new JMenu(configsMap.get("overview.menu.pricelist"));
		mnPriceList.setFont(new Font("Dialog", Font.PLAIN, 12));
		menuBar.add(mnPriceList);

		mntmUpdatePrices = new JMenuItem(configsMap.get("overview.menu.item.updateprices"));
		mntmUpdatePrices.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				priceRetrieving = new Thread() {

					@Override
					public void run() {
						try {
							loadingWindow.getProgressBar().setMinimum(0);
							loadingWindow.getProgressBar().setMaximum(account.getGames().size());
							loadingWindow.setTitle("Updating Prices for " + accountIdentification);
							loadingWindow.setTarget(priceRetrieving);
							loadingWindow.setVisible(true);
							retriever.retrievePricesForGameList(account.getGames(), progressBar);
							loadingWindow.setVisible(false);
							calculateHoursAndPricesTotals();
							fillListAndLabelsForAccount();
						} catch (IOException e) {
							logger.error("Error while retrieving prices", e);
						}
					}

				};
				priceRetrieving.start();
			}
		});
		mntmUpdatePrices.setFont(new Font("Dialog", Font.PLAIN, 12));
		mnPriceList.add(mntmUpdatePrices);

		mntmRefreshPriceList = new JMenuItem(configsMap.get("overview.menu.item.refreshprices"));
		mntmRefreshPriceList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fillListAndLabelsForAccount();
			}
		});
		mntmRefreshPriceList.setFont(new Font("Dialog", Font.PLAIN, 12));
		mnPriceList.add(mntmRefreshPriceList);

		JMenu mnSelectedGame = new JMenu(configsMap.get("overview.menu.selectedgame"));
		mnSelectedGame.setFont(new Font("Dialog", Font.PLAIN, 12));
		menuBar.add(mnSelectedGame);

		JMenuItem mntmViewPriceHistory = new JMenuItem(configsMap.get("overview.menu.item.pricehistory"));
		mntmViewPriceHistory.setFont(new Font("Dialog", Font.PLAIN, 12));
		mntmViewPriceHistory.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				PriceHistoryWindow priceHistoryWindow = new PriceHistoryWindow(selectedGame);
				TableModel priceHistoryTableModel = retriever.getPriceHistoryTableDataForGame(selectedGame);
				priceHistoryWindow.getTable().setModel(priceHistoryTableModel);
				priceHistoryWindow.validate();
			}
		});
		mnSelectedGame.add(mntmViewPriceHistory);

		mntmVisitStorePage = new JMenuItem(configsMap.get("overview.menu.item.storepage"));
		mntmVisitStorePage.setFont(new Font("Dialog", Font.PLAIN, 12));
		mntmVisitStorePage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Desktop.getDesktop().browse(new URI(selectedGame.getStoreLink()));
				} catch (Exception e1) {
					logger.error("Error while opening URL: {}", selectedGame.getStoreLink(), e1);
				}
			}
		});
		mnSelectedGame.add(mntmVisitStorePage);

		mnHelp = new JMenu(configsMap.get("overview.menu.help"));
		mnHelp.setFont(new Font("Dialog", Font.PLAIN, 12));
		menuBar.add(mnHelp);

		mntmHowToUse = new JMenuItem(configsMap.get("overview.menu.item.howtouse"));
		mntmHowToUse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				HowToUseWindow howToUseWindow = new HowToUseWindow();
				howToUseWindow.setVisible(true);
			}
		});
		mntmHowToUse.setFont(new Font("Dialog", Font.PLAIN, 12));
		mnHelp.add(mntmHowToUse);

		separator_1 = new JSeparator();
		mnHelp.add(separator_1);

		mntmAboutSteamProfile = new JMenuItem(configsMap.get("overview.menu.item.about"));
		mntmAboutSteamProfile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AboutWindow aboutWindow = new AboutWindow();
				aboutWindow.setVisible(true);
			}
		});
		mntmAboutSteamProfile.setFont(new Font("Dialog", Font.PLAIN, 12));
		mnHelp.add(mntmAboutSteamProfile);
	}

	private void setupGameListPanel() {

		list = new JList();
		list.setBounds(483, 11, 374, 452);
		list.setSelectionBackground(SystemColor.control);
		list.setForeground(Color.WHITE);
		list.setBackground(Color.DARK_GRAY);
		list.setFont(new Font("Dialog", Font.PLAIN, 12));
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setModel(new AbstractListModel() {
			private static final long serialVersionUID = 6333470967050403599L;
			String[] values = new String[] { "Game 1", "Game 2" };

			public int getSize() {
				return values.length;
			}

			public String getElementAt(int index) {
				return values[index];
			}
		});
		list.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent arg0) {
				if (list.getSelectedValue() != null) {
					updateSelectedItem(list.getSelectedValue());
					gameImagePanel.repaint();
				}
			}
		});
		JScrollPane gameListScrollPane = new JScrollPane(list);
		gameListScrollPane.setBackground(Color.DARK_GRAY);
		gameListScrollPane.setBounds(474, 12, 383, 475);
		mainPanel.add(gameListScrollPane);
		gameListScrollPane.setBorder(new TitledBorder(new LineBorder(new Color(192, 192, 192)), configsMap
				.get("overview.list.gamelist.title"), TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 255, 255)));

		selectedGameDetailsPanel = new JPanel();
		selectedGameDetailsPanel.setBackground(Color.DARK_GRAY);
		selectedGameDetailsPanel.setBounds(5, 92, 210, 144);
		accountSelectedGamePanel_1.add(selectedGameDetailsPanel);
		selectedGameDetailsPanel.setLayout(new GridLayout(6, 1, 0, 0));

		lblHoursPlayed = new JLabel(configsMap.get("overview.label.selected.hoursplayed"));
		selectedGameDetailsPanel.add(lblHoursPlayed);
		lblHoursPlayed.setForeground(Color.WHITE);
		lblHoursPlayed.setVerticalAlignment(SwingConstants.BOTTOM);

		JPanel panelHoursPlayedOverride = new JPanel();
		panelHoursPlayedOverride.setBackground(Color.DARK_GRAY);
		selectedGameDetailsPanel.add(panelHoursPlayedOverride);
		panelHoursPlayedOverride.setLayout(new GridLayout(0, 2, 0, 0));

		txtHours = new JTextField();
		txtHours.setForeground(Color.WHITE);
		txtHours.setBackground(Color.GRAY);
		txtHours.setText("hours");
		panelHoursPlayedOverride.add(txtHours);
		txtHours.setColumns(10);

		JButton btnSaveHours = new JButton("Save Hours");
		btnSaveHours.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				selectedGame.setHoursOnRecord(Double.parseDouble(txtHours.getText().replace(",", "."))); // TODO check input!
				selectedGame.setHourValue(HelperUtil.calculateHourValueForGame(selectedGame.getPrice(),
						selectedGame.getHoursOnRecord()));
				retriever.updateHoursInDatabase(selectedGame, account);
				calculateHoursAndPricesTotals();
				fillListAndLabelsForAccount();
				lblEntertainmentCostPerHourGame.setText(HelperUtil.formatDoubleToCurrency(selectedGame.getHourValue(),
						selectedGame.getCurrency()));
			}
		});
		btnSaveHours.setFont(new Font("Tahoma", Font.PLAIN, 11));
		panelHoursPlayedOverride.add(btnSaveHours);

		lblPricePayed = new JLabel(configsMap.get("overview.label.selected.pricepayed"));
		selectedGameDetailsPanel.add(lblPricePayed);
		lblPricePayed.setForeground(Color.WHITE);
		lblPricePayed.setVerticalAlignment(SwingConstants.BOTTOM);

		JPanel panelPricePayedOverride = new JPanel();
		panelPricePayedOverride.setBackground(Color.DARK_GRAY);
		selectedGameDetailsPanel.add(panelPricePayedOverride);
		panelPricePayedOverride.setLayout(new GridLayout(0, 2, 0, 0));

		txtPrice = new JTextField(configsMap.get("overview.label.notavailable"));
		txtPrice.setForeground(Color.WHITE);
		txtPrice.setBackground(Color.GRAY);
		txtPrice.setText("price");
		panelPricePayedOverride.add(txtPrice);
		txtPrice.setColumns(10);

		JButton btnSavePrice = new JButton("Save Price");
		btnSavePrice.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				selectedGame.setPrice(Double.parseDouble(txtPrice.getText().replace(",", ".")));
				selectedGame.setHourValue(HelperUtil.calculateHourValueForGame(selectedGame.getPrice(),
						selectedGame.getHoursOnRecord()));
				retriever.updatePriceInDatabase(selectedGame, account);
				calculateHoursAndPricesTotals();
				fillListAndLabelsForAccount();
				lblEntertainmentCostPerHourGame.setText(HelperUtil.formatDoubleToCurrency(selectedGame.getHourValue(),
						selectedGame.getCurrency()));
			}
		});
		btnSavePrice.setFont(new Font("Tahoma", Font.PLAIN, 11));
		panelPricePayedOverride.add(btnSavePrice);

		lblEntertainmentCostPer_1 = new JLabel(configsMap.get("overview.label.selected.costperhour"));
		selectedGameDetailsPanel.add(lblEntertainmentCostPer_1);
		lblEntertainmentCostPer_1.setForeground(Color.WHITE);
		lblEntertainmentCostPer_1.setVerticalAlignment(SwingConstants.BOTTOM);

		lblEntertainmentCostPerHourGame = new JLabel(configsMap.get("overview.label.notavailable"));
		selectedGameDetailsPanel.add(lblEntertainmentCostPerHourGame);
		lblEntertainmentCostPerHourGame.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblEntertainmentCostPerHourGame.setForeground(Color.WHITE);
		lblEntertainmentCostPerHourGame.setVerticalAlignment(SwingConstants.TOP);
	}

	protected void updateSelectedItem(Object value) {
		String selected = value.toString();

		for (Game game : account.getGames()) {
			if (game.getName().equals(selected)) {
				selectedGame = game;
				txtHours.setText(Double.toString(game.getHoursOnRecord()));

				if (game.getPrice() != null && game.getCurrency() != null && !game.getPrice().equals(0.0)) {
					txtPrice.setText(Double.toString(game.getPrice()) + " " + game.getCurrency());
				} else if (game.getPrice() != null && game.getCurrency() != null && game.getPrice().equals(0.0)) {
					txtPrice.setText(configsMap.get("overview.label.free.game"));
				} else {
					txtPrice.setText(configsMap.get("overview.label.price.unknown"));
				}

				if (game.getCurrency() != null) {
					lblEntertainmentCostPerHourGame.setText(HelperUtil.formatDoubleToCurrency(game.getHourValue(),
							"" + game.getCurrency()));
				} else {
					lblEntertainmentCostPerHourGame.setText(configsMap.get("overview.label.currency.unknown"));
				}

				break;
			}

		}

	}

	public boolean hasNoAlias() {
		return hasNoAlias;
	}

	public void setHasNoAlias(boolean hasNoAlias) {
		this.hasNoAlias = hasNoAlias;
	}
}
